#cleaning the data
#filter food courts whose licsense status is active and VIOLDTTM in 2018.


boston_food <- mayorsfoodcourt %>% filter(str_detect(VIOLDTTM, "2018")) %>% filter(LICSTATUS=="Active") %>% separate(Location, c("lat","long"),",") %>% filter(!is.na(long) & !is.na(lat))
## Warning: Expected 2 pieces. Missing pieces filled with `NA` in 12663 rows
## [31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 101, 154,
## 155, 156, ...].
#covert location into latitude and longtitude. 
boston_food$lat <- as.numeric(gsub("\\(", "", boston_food$lat))
boston_food$long <- as.numeric(gsub(")", "", boston_food$long))
#View(boston_food)
#leaflet 
data(boston_food)
## Warning in data(boston_food): data set 'boston_food' not found
boston.500 <- boston_food[1:500,]
getColor <- function(boston_food) {
  sapply(boston_food$ViolStatus, function(ViolStatus) {
  if(ViolStatus == "Pass") {
    "green"
  } else if(ViolStatus == "Fail"){
    "red"
  } })
}
icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(boston.500)
)

# Show first 200 rows from the boston_food dataset
leaflet(boston.500) %>% addTiles() %>% addAwesomeMarkers(~long, ~lat, icon=icons, popup = ~as.character(businessName), label = ~as.character(ViolStatus)) 
#%>% addProviderTiles(providers$Esri.NatGeoWorldMap) 
#%>% addLegend("bottomright",colors = ViolStatus, values = ~ViolStatus,title = "Violstatus", opacity = 1)
boston_map<- make_bbox(lat = lat, lon = long, data = boston_food)
boston_map <- get_map(location = boston_map, source = "google", maptype = "hybrid")
## Warning: bounding box given to google - spatial extent only approximate.
## converting bounding box to center/zoom specification. (experimental)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=42.315345,-71.086035&zoom=12&size=640x640&scale=2&maptype=hybrid&language=en-EN&sensor=false
ggmap(boston_map) + 
  geom_point(data = boston_food, mapping = aes(x = long, y = lat, color= ViolStatus))